import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.TextAnchor;

/**
 * 
 * A simple demonstration application showing how to create a line chart using
 * 
 * data from an {@link XYDataset}.
 * 
 *
 */

public class LineChartWithMultiAxis extends ApplicationFrame {

	/**
	 * 
	 * Creates a new demo.
	 * 
	 *
	 * 
	 * @param title
	 * 
	 *            the frame title.
	 */

	public LineChartWithMultiAxis(final String title) {

		super(title);

		final XYDataset dataset = createDataset();

		final JFreeChart chart = createChart(dataset);

		final ChartPanel chartPanel = new ChartPanel(chart);

		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

		setContentPane(chartPanel);

	}

	/**
	 * 
	 * Creates a sample dataset.
	 * 
	 *
	 * 
	 * @return a sample dataset.
	 */

	private XYDataset createDataset() {

		final XYSeries series1 = new XYSeries(

		" Accumulated minimum guaranteed benefit");
		series1.add(1,20000);
		series1.add(2,40600);
		series1.add(3,61818);
		series1.add(4,83673);
		series1.add(5,106183);
		series1.add(6,129368);
		series1.add(7,153249);
		series1.add(8,177847);
		series1.add(9,203182);

        /*

		series1.add(1, 20000);

		series1.add(2, 40000);

		series1.add(3, 60000);

		series1.add(4, 80000);

		series1.add(5, 100000);

		series1.add(6, 120000);

		series1.add(7, 140000);

		series1.add(8, 160000);

		series1.add(9, 180000);

		series1.add(10, 2000000);*/

		final XYSeriesCollection dataset = new XYSeriesCollection();

		dataset.addSeries(series1);

		return dataset;

	}

	private XYDataset createDataset1() {
		final XYSeries series2 = new XYSeries(" Accumulated estimated benefit");
		
		series2.add(1,20000+25000);
		series2.add(2,40600+25000);
		series2.add(3,61818+25000);
		series2.add(4,83673+25000);
		series2.add(5,106183+25000);
		series2.add(6,129368+25000);
		series2.add(7,168249+25000);
		series2.add(8,208297+25000);
		series2.add(9,249546+25000);


		/*
		series2.add(1, 20000 + 250000);

		series2.add(2, 40000 + 250000);

		series2.add(3, 60000 + 250000);

		series2.add(4, 80000 + 250000);

		series2.add(5, 100000 + 250000);

		series2.add(6, 120000 + 250000);

		series2.add(7, 155000 + 250000);

		series2.add(8, 190000 + 250000);

		series2.add(9, 225000 + 250000);

		series2.add(10, 2210000 + 250000);*/

		final XYSeriesCollection dataset = new XYSeriesCollection();

		dataset.addSeries(series2);

		return dataset;

	}

	/**
	 * 
	 * Creates a sample dataset.
	 * 
	 *
	 * 
	 * @return a sample dataset.
	 */

	private XYDataset createDataset2() {

		final XYSeries series3 = new XYSeries("Accumulated maximum benefit");
		series3.add(1,20000+50000);
		series3.add(2,40600+50000);
		series3.add(3,61818+50000);
		series3.add(4,83673+50000);
		series3.add(5,106183+50000);
		series3.add(6,129368+50000);
		series3.add(7,171249+50000);
		series3.add(8,214387+50000);
		series3.add(9,258818+50000);


		/*
		series3.add(1, 20000 + 500000);

		series3.add(2, 40000 + 500000);

		series3.add(3, 60000 + 500000);

		series3.add(4, 80000 + 500000);

		series3.add(5, 100000 + 500000);

		series3.add(6, 120000 + 500000);

		series3.add(7, 158000 + 500000);

		series3.add(8, 196000 + 500000);

		series3.add(9, 234000 + 500000);

		series3.add(10, 2252000 + 500000);*/

		final XYSeriesCollection dataset = new XYSeriesCollection();

		dataset.addSeries(series3);

		return dataset;

	}

	/**
	 * 
	 * Creates a chart.
	 * 
	 *
	 * 
	 * @param dataset
	 * 
	 *            the data for the chart.
	 * 
	 *
	 * 
	 * @return a chart.
	 */

	private JFreeChart createChart(final XYDataset dataset) {

		// create the chart...

		final JFreeChart chart = ChartFactory.createXYLineChart(

		"Accumulated benifit left on deposit", // chart title

				"X", // x axis label

				"Y", // y axis label

				dataset, // data

				PlotOrientation.VERTICAL, true, // include legend

				true, // tooltips

				false // urls

				);

		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

		chart.setBackgroundPaint(Color.white);

		/*
		 * 
		 * final StandardLegend legend = (StandardLegend) chart.getLegend();
		 * 
		 * legend.setDisplaySeriesShapes(true);
		 */

		// get a reference to the plot for further customisation...

		final XYPlot plot = chart.getXYPlot();

		plot.setBackgroundPaint(Color.white);

		// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));

		plot.setDomainGridlinePaint(Color.lightGray);

		plot.setRangeGridlinePaint(Color.lightGray);

		plot.getRangeAxis().setVisible(false);

		final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

		renderer.setSeriesLinesVisible(0, true);

		renderer.setSeriesLinesVisible(1, true);

		renderer.setSeriesLinesVisible(2, true);

		renderer.setSeriesShapesVisible(0, false);

		renderer.setSeriesShapesVisible(1, false);

		renderer.setSeriesShapesVisible(2, false);

		renderer.setBaseItemLabelsVisible(true);

		renderer.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot

		.getLegendItems()));

		plot.setRenderer(renderer);

		// change the auto tick unit selection to integer units only...

		// final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

		// rangeAxis.setTickUnit(new NumberTickUnit(1000));

		// rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		// OPTIONAL CUSTOMISATION COMPLETED.

		return chart;

	}

	// ****************************************************************************

	// * JFREECHART DEVELOPER GUIDE *

	// * The JFreeChart Developer Guide, written by David Gilbert, is available

	// *

	// * to purchase from Object Refinery Limited: *

	// * *

	// * http://www.object-refinery.com/jfreechart/guide.html *

	// * *

	// * Sales are used to provide funding for the JFreeChart project - please *

	// * support us so that we can continue developing free software. *

	// ****************************************************************************

	/**
	 * 
	 * Starting point for the demonstration application.
	 * 
	 *
	 * 
	 * @param args
	 * 
	 *            ignored.
	 */

	/*
	 * 
	 * public static void main(final String[] args) {
	 * 
	 * 
	 * 
	 * final LineChartDemo6 demo = new
	 * 
	 * LineChartDemo6("Accumulated benifit left on deposit"); demo.pack();
	 * 
	 * RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true);
	 * 
	 * 
	 * 
	 * }
	 */

	private void createFrame() {

		NumberAxis xAxis = new NumberAxis("End Of Policy Year");

		xAxis.setRange(Double.parseDouble(".5"), Double.parseDouble("10"));

		xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		xAxis.setTickLabelFont(new Font("Georgia", Font.BOLD, 11));

		// LogAxis yAxis = new LogAxis("Value");

		NumberAxis yAxis = new NumberAxis(
				"Value-Accumulated minimum guaranteed benefit");

		yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		yAxis.setTickUnit(new NumberTickUnit(25000));

		yAxis.setTickLabelsVisible(true);

		yAxis.setNumberFormatOverride(new DecimalFormat("00"));

		yAxis.setTickLabelFont(new Font("Georgia", Font.BOLD, 11));
		yAxis.setLabelPaint(new java.awt.Color(51, 255, 0));

		yAxis.setTickLabelPaint(new java.awt.Color(51, 255, 0));

		yAxis.setRange(0, 275000 + 50000);

		NumberAxis yAxis1 = new NumberAxis(
				"Value-Accumulated estimated benefit");

		yAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		yAxis1.setTickUnit(new NumberTickUnit(25000));

		yAxis1.setTickLabelsVisible(true);

		yAxis1.setNumberFormatOverride(new DecimalFormat("00"));

		yAxis1.setTickLabelFont(new Font("Georgia", Font.BOLD, 11));

		yAxis1.setRange(0, 275000 + 50000);

		yAxis1.setLabelPaint(new java.awt.Color(204, 51, 51));

		yAxis1.setTickLabelPaint(new java.awt.Color(204, 51, 51));
		yAxis1.setNumberFormatOverride(new DecimalFormat() {
			public StringBuffer format(double number, StringBuffer toAppendTo,
					FieldPosition pos) {
				return toAppendTo.append((number - 25000) < 0 ? ""
						: (number - 25000) == 0 ? 0 : (number - 25000));
			}
		});

		NumberAxis yAxis2 = new NumberAxis("Value-Accumulated maximum benefit");

		yAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		yAxis2.setTickUnit(new NumberTickUnit(25000));

		yAxis2.setTickLabelsVisible(true);

		yAxis2.setNumberFormatOverride(new DecimalFormat("00"));

		yAxis2.setTickLabelFont(new Font("Georgia", Font.BOLD, 11));

		yAxis2.setRange(0, 275000 + 50000);

		yAxis2.setLabelPaint(new java.awt.Color(51, 102, 255));

		yAxis2.setTickLabelPaint(new java.awt.Color(51, 102, 255));
		yAxis2.setNumberFormatOverride(new DecimalFormat() {
			public StringBuffer format(double number, StringBuffer toAppendTo,
					FieldPosition pos) {
				return toAppendTo.append((number - 50000) < 0 ? ""
						: (number - 50000) == 0 ? 0 : (number - 50000));
			}
		});

		// plot.setRangeAxis(2, axis3);

		// yAxis.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));

		final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

		renderer.setSeriesLinesVisible(0, true);

		// renderer.setSeriesLinesVisible(1, true);

		// renderer.setSeriesLinesVisible(2, true);

		renderer.setSeriesShapesVisible(0, false);

		// renderer.setSeriesShapesVisible(1, false);

		// renderer.setSeriesShapesVisible(2, false);

		renderer.setSeriesPaint(0, new java.awt.Color(51, 255, 0));

		// renderer.setSeriesPaint(1, new java.awt.Color(204, 51, 51));

		// renderer.setSeriesPaint(2, new java.awt.Color(51, 102, 255));

		renderer.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition(

		ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER));

		/*
		 * renderer.setSeriesPositiveItemLabelPosition(1, new ItemLabelPosition(
		 * 
		 * ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER));
		 * 
		 * renderer.setSeriesPositiveItemLabelPosition(2, new ItemLabelPosition(
		 * 
		 * ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_RIGHT));
		 */

		// XYItemLabelGenerator itemLabelGenerator = new

		// StandardXYItemLabelGenerator(

		// "{2}", new DecimalFormat("00"), new DecimalFormat("00"));

		renderer.setItemLabelFont(new Font("Georgia", Font.BOLD, 11));

		renderer.setBaseItemLabelsVisible(true);

		XYPlot plot = new XYPlot(createDataset(), xAxis, yAxis, renderer);

		renderer.setBaseItemLabelsVisible(true);

		renderer.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot

		.getLegendItems()));

		plot.getRangeAxis().setVisible(true);

		plot.setOrientation(PlotOrientation.VERTICAL);

		plot.setDomainGridlinePaint(Color.white);

		plot.setRangeGridlinePaint(Color.white);

		// ----------------------------------------------------

		plot.setRangeAxis(1, yAxis1);

		plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);

		plot.setDataset(1, createDataset1());

		plot.mapDatasetToRangeAxis(1, 1);

		XYItemRenderer renderer2 = new StandardXYItemRenderer();

		renderer2.setSeriesPaint(0, new java.awt.Color(204, 51, 51));

		renderer2.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition(
				ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER));
		renderer2.setItemLabelFont(new Font("Georgia", Font.BOLD, 11));
		// renderer2.setBase
		renderer2.setBaseItemLabelsVisible(true);
		renderer2.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot

		.getLegendItems(), 25000));
		plot.setRenderer(1, renderer2);

		// ----------------------------------------------------
		// ----------------------------------------------------

		plot.setRangeAxis(2, yAxis2);

		plot.setRangeAxisLocation(2, AxisLocation.BOTTOM_OR_LEFT);

		plot.setDataset(2, createDataset2());

		plot.mapDatasetToRangeAxis(2, 2);

		XYItemRenderer renderer3 = new StandardXYItemRenderer();

		renderer3.setSeriesPaint(0, new java.awt.Color(51, 102, 255));

		renderer3.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition(
				ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER));
		renderer3.setItemLabelFont(new Font("Georgia", Font.BOLD, 11));
		// renderer2.setBase
		renderer3.setBaseItemLabelsVisible(true);
		renderer3.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot

		.getLegendItems(), 50000));
		plot.setRenderer(2, renderer3);

		// ----------------------------------------------------

		JFreeChart chart = new JFreeChart(

		"Accumulated benifit left on deposit",

		JFreeChart.DEFAULT_TITLE_FONT, plot, false);

		JFrame frame = new JFrame("LogAxis Test");

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setContentPane(new ChartPanel(chart));

		frame.pack();

		frame.setVisible(true);

	}

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {

				final LineChartWithMultiAxis demo = new LineChartWithMultiAxis(

				"Accumulated Benifit left on deposit");

				demo.createFrame();

			}

		});

	}

}